package com.hudongwx.food.fargment;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import com.hudongwx.food.R;
import com.hudongwx.food.activity.MainActivity;
import com.hudongwx.food.adapter.MyListViewAdapter1;
import com.hudongwx.food.adapter.MyListViewAdapter2;
import com.hudongwx.food.adapter.MygridviewAdapter;
import com.hudongwx.food.item.MyGridView;
import com.hudongwx.food.utils.API;
import com.hudongwx.food.utils.MyJsonObject;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;
import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.ArrayList;
import java.util.List;


/**
 * 首页
 * A simple {@link Fragment} subclass.
 */
@ContentView(R.layout.fragment_homepage)
public class HomepageFragment extends Fragment {
    private View view = null;
    private MyAdapter myadapter;
    private JSONArray img, gidview, listview1, listview2;
    @ViewInject(R.id.home_recyclerView)
    private RecyclerView recyclerView;
    private String TAG = "HomepageFragment";
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (null==view){
            view = x.view().inject(this, inflater, container);
            GridLayoutManager manager = new GridLayoutManager(getActivity(), 1);
            recyclerView.setLayoutManager(manager);
            int spacingInPixels = getResources().getDimensionPixelSize(R.dimen.activity_space);
            recyclerView.addItemDecoration(new SpaceItemDecoration(spacingInPixels));
            myadapter=new MyAdapter(img, gidview, listview1, listview2);
            recyclerView.setAdapter(myadapter);
            Log.i(TAG, "onCreateView: " + recyclerView.getId());
            getitem();
            getitem1();
            getitem2();
            getitem3();
        }


        return view;

    }

    class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
         JSONArray img, gidview, listview1, listview2;

        public MyAdapter(JSONArray img, JSONArray gidview, JSONArray listview1, JSONArray listview2) {
            this.img = img;
            this.gidview = gidview;
            this.listview1 = listview1;
            this.listview2 = listview2;
        }

        public void updateImg(JSONArray img) {
            this.img = img;
            notifyDataSetChanged();
        }

        public void updateGidView(JSONArray gidview) {
            this.gidview = gidview;
            Log.i(TAG, "updateGidView: "+gidview.length());
            notifyDataSetChanged();
        }

        public void updateListView1(JSONArray listview1) {
            this.listview1 = listview1;
            notifyDataSetChanged();
        }

        public void updateListView2(JSONArray listview2) {
            this.listview2 = listview2;
            notifyDataSetChanged();
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder myViewHolder = null;
            switch (viewType) {
                case 0:
                    View view = LayoutInflater.from(getActivity()).inflate(R.layout.home_animation, null);
                    myViewHolder = new MyViewHolder(view);
                    break;
                case 1:
                    View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.home_classification, null);
                    myViewHolder = new MyViewHolder(view1);
                    break;
                case 2:
                    View view2 = LayoutInflater.from(getActivity()).inflate(R.layout.home_blogs, null);
                    myViewHolder = new MyViewHolder(view2);
                    break;
                case 3:
                    View view3 = LayoutInflater.from(getActivity()).inflate(R.layout.homr_topics, null);
                    myViewHolder = new MyViewHolder(view3);
                    break;
            }
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {

            switch (position) {
                case 0:
                    if (null == img) {
                        holder.pagerView.setVisibility(View.GONE);
                    } else {
                        holder.iv.setVisibility(View.GONE);
                        holder.pagerView.setVisibility(View.VISIBLE);
                        //设置播放时间间隔
                        holder.pagerView.setPlayDelay(1000);
                        //设置透明度
//                        holder.pagerView.setAnimationDurtion(500);
                        //设置适配器

                        //设置指示器（顺序依次）
                        //自定义指示器图片
                        //设置圆点指示器颜色
                        //设置文字指示器
                        //隐藏指示器
                        //mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
                        //mRollViewPager.setHintView(new TextHintView(this));
                        //mRollViewPager.setHintView(null);
                        List<String> list = new ArrayList<>();

                        for (int i = 0; i < img.length(); i++) {
                            try {
//                                ImageView iv1 = new ImageView(getActivity());
//                                utils.display(iv1,img.getJSONObject(i).getString("PageUrl"),config);
                                list.add(img.getJSONObject(i).getString("Url"));
                                Log.i(TAG, "onBindViewHolder: "+img.getJSONObject(i).getString("Url"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        TestNormalAdapter testNormalAdapter = new TestNormalAdapter(list);
                        holder.pagerView.setAdapter(testNormalAdapter);

                        holder.pagerView.setHintView(new ColorPointHintView(getActivity().getBaseContext(), Color.YELLOW,Color.WHITE));

                    }
                    break;
                case 1:
                    Log.i(TAG, "onBindViewHolder: >>>>>>>>>>>>>>>>>>>>>>>>>.");
                    if (null == gidview) {
                        holder.myGridView.setVisibility(View.GONE);
                    } else {
                        holder.myGridView.setVisibility(View.VISIBLE);
                        holder.myGridView.setAdapter(new MygridviewAdapter(gidview,getActivity()));
                    }
                    break;
                case 2:
                    if (null==listview1){
                        holder.listView.setVisibility(View.GONE);
                    }else {
                        holder.listView.setVisibility(View.VISIBLE);
                        holder.listView.setAdapter(new MyListViewAdapter1(listview1,getActivity()));
                    }
                    break;
                case 3:
                    if (null==listview2){
                        holder.listView1.setVisibility(View.GONE);
                    }else {
                        holder.listView1.setVisibility(View.VISIBLE);
                        holder.listView1.setAdapter(new MyListViewAdapter2(listview2,getActivity()));
                    }
                    break;
            }
        }

        @Override
        public int getItemCount() {
            return 4;
        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private ListView listView;
        private ListView listView1;
        private MyGridView myGridView;
        private ImageView iv;
        RollPagerView pagerView;

        public MyViewHolder(View itemView) {
            super(itemView);
            iv = (ImageView) itemView.findViewById(R.id.home_1);
            myGridView = (MyGridView) itemView.findViewById(R.id.MyGridView);
            listView = (ListView) itemView.findViewById(R.id.home_blogs);
            listView1 = (ListView) itemView.findViewById(R.id.home_topics_listView);
            pagerView = (RollPagerView) itemView.findViewById(R.id.roll_view_pager);
        }
    }

    public void getitem() {

        RequestParams params = new RequestParams(API.getFoodMenu());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");

                    myadapter.updateGidView(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    public void getitem1() {

        RequestParams params = new RequestParams(API.getCommunity());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    myadapter.updateListView1(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
                Log.e(TAG, "onFinished: 11111111111111111111111111111");
            }
        });
    }
    public void getitem2() {

        RequestParams params = new RequestParams(API.getSpecial());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");

                    myadapter.updateListView2(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    public class SpaceItemDecoration extends RecyclerView.ItemDecoration{

        private int space;

        public SpaceItemDecoration(int space) {
            this.space = space;
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

            if(parent.getChildPosition(view) != 0)
                outRect.top = space;
        }
    }
    private class TestNormalAdapter extends StaticPagerAdapter {
        public TestNormalAdapter(List<String> imgs) {
            this.imgs = imgs;
        }

        private List<String> imgs = new ArrayList<>();

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view1=new ImageView(container.getContext());
//            BitmapUtils utils = new BitmapUtils(getActivity());
//            BitmapDisplayConfig config = new BitmapDisplayConfig();
//            utils.display(view,imgs.get(position),config);
            x.image().bind(view1,imgs.get(position));
            view1.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view1.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view1;
        }


        @Override
        public int getCount() {
            return imgs.size();
        }
    }
    public void getitem3() {

        RequestParams params = new RequestParams(API.getViewPagerImg());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    myadapter.updateImg(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }
}
